package com.by_man.presenter;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Process;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.widget.ImageView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.by_man.presenter.module_health_consultant.BitmapCache;
import com.by_man.repository.PostJsonArrayRequest;
import com.by_man.repository.PostStringRequest;
import com.squareup.leakcanary.LeakCanary;

import org.json.JSONArray;
import org.json.JSONObject;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 自定义Application，提供通用的方法及数据
 * Created by man on 2017-01-31.
 */
public class SharedApplication extends Application {

    @Override public void onCreate() {
        super.onCreate();
        if (LeakCanary.isInAnalyzerProcess(this)) {
            // This process is dedicated to LeakCanary for heap analysis.
            // You should not init your app in this process.
            return;
        }
        LeakCanary.install(this);
        // Normal app init code...
    }

    /*************************************************************************************************************
     * 下面为一些通用的方法
     * by man
     * 2017-01-31
     **************************************************************************************************************/

    // 密码加密
    public static String MD5(String password, int i) {
        String result = "";
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(password.getBytes());
            byte b[] = md.digest();
            StringBuffer buf = new StringBuffer("");
            for (int offset = 0; offset < b.length; offset++) {
                i = b[offset];
                if (i < 0)
                    i += 256;
                if (i < 16)
                    buf.append("0");
                buf.append(Integer.toHexString(i));
            }
            result = buf.toString();
            System.out.println("MD5(" + password + ",32) = " + result);
            System.out.println("MD5(" + password + ",16) = " + buf.toString().substring(8, 24));
        } catch (NoSuchAlgorithmException e) {
            System.out.println(e);
        }
        return result;
    }

    //提示信息窗口
    public static void showToast(final Activity activity, final String message){
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(activity,message,Toast.LENGTH_SHORT).show();
            }
        });
    }

    public static RequestQueue rQueue;

    public static ImageLoader imageLoader;

    // 统一让此队列持有context，以期解决内存泄漏的问题
    public static RequestQueue getrQueue(Context context) {
        if (rQueue == null) {
            rQueue = Volley.newRequestQueue(context);
        }
        return rQueue;
    }

    public static ImageLoader getImageLoader(Context context) {
        if (imageLoader == null) {
            imageLoader = new ImageLoader(getrQueue(context), new BitmapCache());
        }
        return imageLoader;
    }

}
